package	com.mes.web.controller.jyy;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.mes.entity.jyy.ItemJy;
import com.mes.entity.jyy.Report;
import com.mes.entity.llm.ChartLlm;
import com.mes.entity.llm.Clazz;
import com.mes.entity.tdd.Staff;
import com.mes.service.jyy.ReportService;
import com.mes.service.llm.ChartLlmService;
import com.mes.service.llm.ClazzService;
import com.mes.utils.DateUtil;
import com.mes.utils.ResponseUtil;
/**
 *
 *@ClassName: ReportController
 *@Description: 财务-报表记录
 *@Author jyy
 *@Date 2019-05-29 13:28:31
 *@version 1.0
*/
@Controller
@RequestMapping("/report")
public class ReportController{
	@Resource ReportService reportService;
	@Resource ClazzService clazzService;
	@Resource ChartLlmService chartLlmService;
	/** 
	 * 进入页面
	 * @param model
	 * @return 页面路径
	*/
	@RequestMapping("/page")
	public String pageReport (Model model) {
		List<ItemJy> list = new ArrayList<ItemJy>();
		ItemJy itemJy = new ItemJy();
		itemJy.setValue("1");
		itemJy.setText("222");
		
		ItemJy itemJy2 = new ItemJy();
		itemJy2.setValue("2");
		itemJy2.setText("333");
		
		
		list.add(itemJy);
		list.add(itemJy2);
		
		model.addAttribute("itemjy", list);
		return "/jy/momv2/report";
	}
	/** 
	 * 根据主键id查询实体
	 * @param id 主键id
	 * @return 实体
	*/
	@RequestMapping("/queryById")
	@ResponseBody
	public Report queryById(Long id) {
		Report reportParam = new Report();
		reportParam.setId(id);
		Report param = reportService.queryById(reportParam );
		return param;
	}
	/** 
	 * 条件查询所有
	 * @param param 实体条件
	 * @return 实体集合
	*/
	@RequestMapping("/queryAllByMution")
	@ResponseBody
	public List<Report> queryAllByMution(Report param) {
		List<Report> list = reportService.queryAllByMution(param);
		return list;
	}
	/** 
	 * 条件分页查询
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/list")
	public void list(Report param,HttpServletResponse response, HttpSession session) throws Exception{
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			
			param.setTablename("jyy_books_report_"+ename);
			param.setTablename2("jyy_books_clazz_"+ename);
		}
		
		/*
		 * 条件分页查询内容
		 */
		List<Report> list = reportService.findPageByMution(param);
		/*
		 * 条件查询总条数
		 */
		Integer total = reportService.selectCountByMution(param);
		
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("total", total);
		jsonObject.put("rows", list);
		ResponseUtil.write(response, jsonObject);
	}
	/** 
	 * 插入返回id
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/insert")
	public void insert(Report param, HttpServletResponse response, HttpSession session) throws Exception{
		double money = param.getMoney();
		Long cid = param.getCid();
		String years = DateUtil.dateFormat();
		String moth = param.getCreateTime().substring(5, 7);
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			param.setCreatePeople(staffid+"");
			
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			
			param.setTablename("jyy_books_report_"+ename);
		}
		Long id = reportService.insert(param);
		
		//向统计表添加数据
		if(id > 0) {
			//添加本科目统计信息
			addSelfChart(years, moth, cid, money, session);
		}
		
		response.getWriter().print(id);
	}
	/** 
	 * 根据主键更新 返回影响行数
	 * @param param 实体条件
	 * @param response
	 * @throws Exception
	*/
	@RequestMapping("/update")
	public void update(Report param,HttpServletResponse response, HttpSession session) throws Exception{
		
		Report reportParam = new Report();
		
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			param.setCreatePeople(staffid+"");
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			
			param.setTablename("jyy_books_report_"+ename);
			reportParam.setTablename("jyy_books_report_"+ename);
		}
		
		
		double money = param.getMoney();
		Long cid = param.getCid();
		String years = DateUtil.dateFormat();
		String moth =DateUtil.dateFormatMoth();
		
		double moneyOld = 0;
		
		//1.查询此科目，原信息，读取原 金额，减去原金额
		
		reportParam.setCid(cid);
		
		List<Report> reports = reportService.queryAllByMution(reportParam );
		if(reports.size() > 0) {
			Report report = reports.get(0);
			if(report != null) {
				moneyOld= report.getMoney();
			}
		}
		
		updateSelfChart(years, moth, cid, money, moneyOld,session);
		Integer count = reportService.update(param);
		
		response.getWriter().print(count);
	}
	/** 
	 * 根据主键拼接的字符串删除返回影响行数
	 * @param ids 主键拼接的字符串
	 * @return 影响行数
	*/
	@RequestMapping("/delete")
	public void delete(String ids,HttpServletResponse response, HttpSession session) throws Exception{
		Report param = new Report();
		String ename = "";
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			param.setCreatePeople(staffid+"");
			//根据员工id查询，该员工账套的英文名称
			ename = reportService.selectBooksEnameBysid(staffid);
			
			param.setTablename("jyy_books_report_"+ename);
		}
		
		String years = DateUtil.dateFormat();
		String moth =DateUtil.dateFormatMoth();
		
		double moneyOld = 0;
		String[] split = ids.split(",");
		for (String s : split) {
			Long id = Long.valueOf(s);
			
			//删除前，先查询此信息，在统计表中删除此信息
			//1.查询此科目，原信息，读取原 金额，减去原金额
			Report reportParam = new Report();
			reportParam.setId(id);
			reportParam.setTablename("jyy_books_report_"+ename);
			Report report = reportService.queryById(reportParam);
			Long cid = report.getCid();
			moneyOld = report.getMoney();
			deleteSelfChart(years, moth, cid, moneyOld, session);
			reportParam = null;
		}
		param.setIds(ids);
		Integer count = reportService.delete(param);
		response.getWriter().print(count);
	}
	
	/**
	 * 查询所有科目
	 * @throws Exception 
	 */
	@RequestMapping("/selectItem")
	public void selectItem(HttpServletResponse response, HttpSession session) throws Exception {
		
		Clazz param = new Clazz();
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			param.setTablename("jyy_books_clazz_"+ename);
		}
		List<Clazz> list = clazzService.queryAllByMution(param);
		
		Gson gson = new Gson();
		String json = gson.toJson(list);
		System.out.println(json);
		ResponseUtil.write(response, json);
	}
	
	/**
	 * 查询所有科目
	 * @throws Exception 
	 */
	@RequestMapping("/selectItem2")
	public void selectItem2(HttpServletResponse response, HttpSession session) throws Exception {
		List<ItemJy> list = new ArrayList<ItemJy>();
		Clazz param = new Clazz();
		
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			String ename = reportService.selectBooksEnameBysid(staffid);
			param.setTablename("jyy_books_clazz_"+ename);
		}
		
		List<Clazz> list2 = clazzService.queryAllByMution(param);
		for (Clazz clazz : list2) {
			ItemJy itemJy = new ItemJy();
			itemJy.setText(clazz.getCode()+clazz.getCname());
			itemJy.setValue(clazz.getId()+"");
			
			list.add(itemJy);
			itemJy = null;
		}
		Gson gson = new Gson();
		String json = gson.toJson(list);
		ResponseUtil.write(response, json);
	}
	
	//----------------------------------------------------------------调用的方法-------------------------------------------------------------
	
	/**
	 * 添加本科目的报表信息
	 * @param years 年份
	 * @param moth 月份
	 * @param cid 科目id
	 * @param money 金额
	 */
	public void addSelfChart(String years, String moth, Long cid, double money, HttpSession session) {
		Long fid = 0L;
		
		String ename = "";
		ChartLlm chartLlm = new ChartLlm();
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			ename = reportService.selectBooksEnameBysid(staffid);
			
			chartLlm.setTablename("jyy_books_chart_"+ename);
		}
		
		//根据科目id查询
		chartLlm.setCid(cid);
		chartLlm.setYears(years);
		List<ChartLlm> llms = chartLlmService.queryAllByMution(chartLlm );
		
		if(llms.size() > 0) {
			//已有，修改累加 月金额 和 年金额	
			ChartLlm chartLlm3 = llms.get(0);
			if("01".equals(moth)) {
				double jan = chartLlm3.getJan();
				chartLlm3.setJan(jan+money);
			}else if("02".equals(moth)) {
				double feb = chartLlm3.getFeb();
				chartLlm3.setFeb(feb+money);
			}else if("03".equals(moth)) {
				double mar = chartLlm3.getMar();
				chartLlm3.setMar(mar+money);
			}else if("04".equals(moth)) {
				double apr = chartLlm3.getApr();
				chartLlm3.setApr(apr + money);
			}else if("05".equals(moth)) {
				double may = chartLlm3.getMay();
				chartLlm3.setMay(may + money);
			}else if("06".equals(moth)) {
				double june = chartLlm3.getJune();
				chartLlm3.setJune(june + money);
			}else if("07".equals(moth)) {
				double july = chartLlm3.getJuly();
				chartLlm3.setJuly(july + money);
			}else if("08".equals(moth)) {
				double aug = chartLlm3.getAug();
				chartLlm3.setAug(aug + money);
			}else if("09".equals(moth)) {
				double sept = chartLlm3.getSept();
				chartLlm3.setSept(sept + money);
			}else if("10".equals(moth)) {
				double oct = chartLlm3.getOct();
				chartLlm3.setOct(oct + money);
			}else if("11".equals(moth)) {
				double dec = chartLlm3.getDec();
				chartLlm3.setDec(dec + money);
			}else if("12".equals(moth)) {
				double nov = chartLlm3.getNov();
				chartLlm3.setNov(nov + money);
			}
			
			//年金额累计
			double price = chartLlm3.getPrice();
			chartLlm3.setPrice(price + money);
			chartLlm3.setTablename("jyy_books_chart_"+ename);
			
			chartLlmService.update(chartLlm3);
		}else {
			ChartLlm chartLlm3 = new ChartLlm();
			
			chartLlm3.setCid(cid);
			//年累计
			chartLlm3.setPrice(money);
			//年份
			chartLlm3.setYears(years);
			
			if("01".equals(moth)) {
				chartLlm3.setJan(money);
			}else if("02".equals(moth)) {
				chartLlm3.setFeb(money);
			}else if("03".equals(moth)) {
				chartLlm3.setMar(money);
			}else if("04".equals(moth)) {
				chartLlm3.setApr(money);
			}else if("05".equals(moth)) {
				chartLlm3.setMay(money);
			}else if("06".equals(moth)) {
				chartLlm3.setJune(money);
			}else if("07".equals(moth)) {
				chartLlm3.setJuly(money);
			}else if("08".equals(moth)) {
				chartLlm3.setAug(money);
			}else if("09".equals(moth)) {
				chartLlm3.setSept(money);
			}else if("10".equals(moth)) {
				chartLlm3.setOct(money);
			}else if("11".equals(moth)) {
				chartLlm3.setDec(money);
			}else if("12".equals(moth)) {
				chartLlm3.setNov(money);
			}
			chartLlm3.setTablename("jyy_books_chart_"+ename);
			chartLlmService.insert(chartLlm3);
		}
		
		
		//查询该科目是否有父级
		Clazz clazz = new Clazz();
		clazz.setId(cid);
		clazz.setTablename("jyy_books_clazz_"+ename);
		List<Clazz> clazzs = clazzService.queryAllByMution(clazz);
		
		if(clazzs.size() > 0) {
			Clazz clazz2 = clazzs.get(0);
			fid = clazz2.getFid();
			if(fid != 0) {
				//有父级
				clazz.setId(fid);
				List<Clazz> clazzs2 = clazzService.queryAllByMution(clazz);
				//判断父级存在不
				if(clazzs2.size() > 0) {
					Long id2 = clazzs2.get(0).getId();
					//根据父级id 添加父级统计信息
					addSelfChart(years, moth, id2, money, session);
				}
			}
		}
	}
	
	/**
	 * 修改本科目的报表信息
	 * @param years 年份
	 * @param moth 月份
	 * @param cid 科目id
	 * @param money 金额
	 */
	public void updateSelfChart(String years, String moth, Long cid, double money, double moneyOld , HttpSession session) {
		Long fid = 0L;
		
		String ename = "";
		ChartLlm chartLlm = new ChartLlm();
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			ename = reportService.selectBooksEnameBysid(staffid);
			
			chartLlm.setTablename("jyy_books_chart_"+ename);
		}
		
		//根据科目id查询
		chartLlm.setCid(cid);
		chartLlm.setYears(years);
		List<ChartLlm> llms = chartLlmService.queryAllByMution(chartLlm );
		
		if(llms.size() > 0) {
			//已有，修改累加 月金额 和 年金额	
			ChartLlm chartLlm3 = llms.get(0);
			if("01".equals(moth)) {
				double jan = chartLlm3.getJan();
				chartLlm3.setJan(jan - moneyOld +money);
			}else if("02".equals(moth)) {
				double feb = chartLlm3.getFeb();
				chartLlm3.setFeb(feb - moneyOld +money);
			}else if("03".equals(moth)) {
				double mar = chartLlm3.getMar();
				chartLlm3.setMar(mar - moneyOld +money);
			}else if("04".equals(moth)) {
				double apr = chartLlm3.getApr();
				chartLlm3.setApr(apr - moneyOld + money);
			}else if("05".equals(moth)) {
				double may = chartLlm3.getMay();
				chartLlm3.setMay(may - moneyOld + money);
			}else if("06".equals(moth)) {
				double june = chartLlm3.getJune();
				chartLlm3.setJune(june - moneyOld + money);
			}else if("07".equals(moth)) {
				double july = chartLlm3.getJuly();
				chartLlm3.setJuly(july - moneyOld + money);
			}else if("08".equals(moth)) {
				double aug = chartLlm3.getAug();
				chartLlm3.setAug(aug - moneyOld + money);
			}else if("09".equals(moth)) {
				double sept = chartLlm3.getSept();
				chartLlm3.setSept(sept - moneyOld + money);
			}else if("10".equals(moth)) {
				double oct = chartLlm3.getOct();
				chartLlm3.setOct(oct - moneyOld + money);
			}else if("11".equals(moth)) {
				double dec = chartLlm3.getDec();
				chartLlm3.setDec(dec - moneyOld + money);
			}else if("12".equals(moth)) {
				double nov = chartLlm3.getNov();
				chartLlm3.setNov(nov - moneyOld + money);
			}
			
			//年金额累计
			double price = chartLlm3.getPrice();
			chartLlm3.setPrice(price - moneyOld + money);
			chartLlm3.setTablename("jyy_books_chart_"+ename);
			chartLlmService.update(chartLlm3);
		}else {
			ChartLlm chartLlm3 = new ChartLlm();
			
			chartLlm3.setCid(cid);
			//年累计
			chartLlm3.setPrice(money);
			//年份
			chartLlm3.setYears(years);
			
			if("01".equals(moth)) {
				chartLlm3.setJan(money);
			}else if("02".equals(moth)) {
				chartLlm3.setFeb(money);
			}else if("03".equals(moth)) {
				chartLlm3.setMar(money);
			}else if("04".equals(moth)) {
				chartLlm3.setApr(money);
			}else if("05".equals(moth)) {
				chartLlm3.setMay(money);
			}else if("06".equals(moth)) {
				chartLlm3.setJune(money);
			}else if("07".equals(moth)) {
				chartLlm3.setJuly(money);
			}else if("08".equals(moth)) {
				chartLlm3.setAug(money);
			}else if("09".equals(moth)) {
				chartLlm3.setSept(money);
			}else if("10".equals(moth)) {
				chartLlm3.setOct(money);
			}else if("11".equals(moth)) {
				chartLlm3.setDec(money);
			}else if("12".equals(moth)) {
				chartLlm3.setNov(money);
			}
			chartLlm3.setTablename("jyy_books_chart_"+ename);
			chartLlmService.insert(chartLlm3);
		}
		
		
		//查询该科目是否有父级
		Clazz clazz = new Clazz();
		clazz.setId(cid);
		clazz.setTablename("jyy_books_clazz_"+ename);
		List<Clazz> clazzs = clazzService.queryAllByMution(clazz);
		
		if(clazzs.size() > 0) {
			Clazz clazz2 = clazzs.get(0);
			fid = clazz2.getFid();
			if(fid != 0) {
				//有父级
				clazz.setId(fid);
				List<Clazz> clazzs2 = clazzService.queryAllByMution(clazz);
				//判断父级存在不
				if(clazzs2.size() > 0) {
					Long id2 = clazzs2.get(0).getId();
					//根据父级id 添加父级统计信息
					updateSelfChart(years, moth, id2, money, moneyOld, session);
				}
			}
		}
	}
	
	/**
	 * 删除（减去）本科目的报表信息
	 * @param years 年份
	 * @param moth 月份
	 * @param cid 科目id
	 * @param money 金额
	 */
	public void deleteSelfChart(String years, String moth, Long cid, double moneyOld, HttpSession session) {
		Long fid = 0L;
		String ename = "";
		ChartLlm chartLlm = new ChartLlm();
		Staff staff = (Staff) session.getAttribute("staff");
		if(staff != null) {
			Long staffid = staff.getStaffid();
			
			//根据员工id查询，该员工账套的英文名称
			ename = reportService.selectBooksEnameBysid(staffid);
			
			chartLlm.setTablename("jyy_books_chart_"+ename);
		}
		
		//根据科目id查询
		chartLlm.setCid(cid);
		chartLlm.setYears(years);
		List<ChartLlm> llms = chartLlmService.queryAllByMution(chartLlm );
		
		if(llms.size() > 0) {
			//已有，修改累加 月金额 和 年金额	
			ChartLlm chartLlm3 = llms.get(0);
			if("01".equals(moth)) {
				double jan = chartLlm3.getJan();
				chartLlm3.setJan(jan - moneyOld);
			}else if("02".equals(moth)) {
				double feb = chartLlm3.getFeb();
				chartLlm3.setFeb(feb - moneyOld);
			}else if("03".equals(moth)) {
				double mar = chartLlm3.getMar();
				chartLlm3.setMar(mar - moneyOld);
			}else if("04".equals(moth)) {
				double apr = chartLlm3.getApr();
				chartLlm3.setApr(apr - moneyOld);
			}else if("05".equals(moth)) {
				double may = chartLlm3.getMay();
				chartLlm3.setMay(may - moneyOld);
			}else if("06".equals(moth)) {
				double june = chartLlm3.getJune();
				chartLlm3.setJune(june - moneyOld);
			}else if("07".equals(moth)) {
				double july = chartLlm3.getJuly();
				chartLlm3.setJuly(july - moneyOld);
			}else if("08".equals(moth)) {
				double aug = chartLlm3.getAug();
				chartLlm3.setAug(aug - moneyOld);
			}else if("09".equals(moth)) {
				double sept = chartLlm3.getSept();
				chartLlm3.setSept(sept - moneyOld);
			}else if("10".equals(moth)) {
				double oct = chartLlm3.getOct();
				chartLlm3.setOct(oct - moneyOld);
			}else if("11".equals(moth)) {
				double dec = chartLlm3.getDec();
				chartLlm3.setDec(dec - moneyOld);
			}else if("12".equals(moth)) {
				double nov = chartLlm3.getNov();
				chartLlm3.setNov(nov - moneyOld);
			}
			
			//年金额累计
			double price = chartLlm3.getPrice();
			chartLlm3.setPrice(price - moneyOld);
			
			chartLlm3.setTablename("jyy_books_chart_"+ename);
			chartLlmService.update(chartLlm3);
		}
		
		//查询该科目是否有父级
		Clazz clazz = new Clazz();
		clazz.setId(cid);
		clazz.setTablename("jyy_books_clazz_"+ename);
		List<Clazz> clazzs = clazzService.queryAllByMution(clazz);
		
		if(clazzs.size() > 0) {
			Clazz clazz2 = clazzs.get(0);
			fid = clazz2.getFid();
			if(fid != 0) {
				//有父级
				clazz.setId(fid);
				List<Clazz> clazzs2 = clazzService.queryAllByMution(clazz);
				//判断父级存在不
				if(clazzs2.size() > 0) {
					Long id2 = clazzs2.get(0).getId();
					//根据父级id 添加父级统计信息
					deleteSelfChart(years, moth, id2, moneyOld, session);
				}
			}
		}
	}
	
}
